package config.db;

import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
import org.springframework.data.mongodb.ReactiveMongoTransactionManager;
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration;
import org.springframework.data.mongodb.config.EnableMongoAuditing;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;

/**
 * mongodb 配置文件
 */
@Configuration
@EnableReactiveMongoRepositories(basePackages = "dao.mongo")
@EnableMongoAuditing
public class MongodbConfiguration extends AbstractReactiveMongoConfiguration {

    @Bean
    public ReactiveMongoTransactionManager transactionManager(ReactiveMongoDatabaseFactory factory) {
        return new ReactiveMongoTransactionManager(factory);
    }

    @Override
    @Bean
    public MongoClient reactiveMongoClient() {
        return MongoClients.create("mongodb://localhost:27017/");
    }


    @Override
    protected String getDatabaseName() {
        return "flux";
    }

    /**
     * 容器启动时不自动创建索引
     * @return
     */
    @Override
    protected boolean autoIndexCreation() {
//        return false;
        return true;
    }
    //    @Bean
//    public MongoTemplate mongoTemplate() {
//
//    }

//    @Override
//    protected Collection<String> getMappingBasePackages() {
//        List<String> list = new ArrayList<>();
//        list.add("dao.mongo");
//        return list;
//    }

//    @Bean
//    public ReactiveMongoTemplate reactiveMongoTemplate(MongoClient mongoClient) {
//        ReactiveMongoTemplate mongoOps = new ReactiveMongoTemplate(mongoClient, "flux");
//        return mongoOps;
//    }
}
